<template>
  <j-modal
    title="驳回领料单"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :span="24">
            <a-form-item label="领料单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input disabled v-decorator="['requisitionNo']" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="驳回原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea
                placeholder="请输入驳回原因"
                v-decorator="['approveComment', { rules: [{ required: true, message: '请输入驳回原因!' }] }]"
                :rows="4"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { FormMixin } from '@/mixins/formMixin'
import { postAction } from '@/api/manage'

export default {
  name: 'RequisitionRejectModal',
  mixins: [FormMixin],
  data() {
    return {
      form: null,
      title: '驳回领料单',
      visible: false,
      model: {},
      labelCol: { span: 5 },
      wrapperCol: { span: 19 },
      confirmLoading: false,
      url: {
        reject: '/wms/requisition/reject',
      },
    }
  },
  mounted() {
    this.form = this.$form.createForm(this)
  },
  methods: {
    show(record) {
      if (!this.form) {
        this.form = this.$form.createForm(this)
      }

      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true

      this.$nextTick(() => {
        this.form.setFieldsValue({
          requisitionNo: this.model.requisitionNo,
          approveComment: '',
        })
      })
    },

    close() {
      this.visible = false
    },

    handleOk() {
      const that = this

      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true

          let formData = {
            id: this.model.id,
            approveComment: values.approveComment,
          }

          postAction(this.url.reject, formData)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                that.close()
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>

<style scoped>
</style> 